package org.hismig.migration.result;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hismig.utils.DateTool;

/**
 * @declare: //TODO <br />
 *           This class created by Vansion.Hopihit on 2012-07-04 <br />
 *           <a href="mailto:vansionhopihit@gmail.com">Contact Me</a>
 * @version 1.0
 * @author Vansion.Hopihit
 * 
 */
public class JobResultInfo extends BaseInfo {

	private Date startDate;
	private Date endDate;

	public JobResultInfo(String name) {
		this.name = name;
	}

	private List<DBResultInfo> dbResultInfos = new ArrayList<DBResultInfo>();

	public List<DBResultInfo> getDbResultInfos() {
		return dbResultInfos;
	}

	public void addDbResultInfo(DBResultInfo dbResultInfo) {
		dbResultInfos.add(dbResultInfo);
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	@Override
	public int getErrorCount() {
		int count = super.getErrorCount();

		for (DBResultInfo dbResultInfo : dbResultInfos) {
			count += dbResultInfo.getErrorCount();
		}

		return count;
	}

	public String formate() {

		StringBuffer sb = new StringBuffer();
		sb.append("<html>");
		sb.append(String.format("<strong>开始时间 : </strong>%s<p>",
				DateTool.getNormalDateTime(startDate)));
		sb.append(String.format("<strong>结束时间 : </strong>%s<p>",
				DateTool.getNormalDateTime(endDate)));
		sb.append(String.format("<strong>数据库数量 : </strong>%s<p>",
				getDbResultInfos().size()));
		
		sb.append(String.format("<strong>耗时 : </strong>%sms<p>",
				getExecuteTime()));

		if (getErrorCount() > 0) {
			sb.append(String.format(
					"<strong><font color='red'>错误 : %s</font></strong><p>",
					getErrorCount()));
		} else {
			sb.append(String.format(
					"<strong><font color='green'>错误 : %s</font></strong><p>",
					getErrorCount()));
		}
		
		sb.append("</html>");

		return sb.toString();
	}

	@Override
	public String toString() {
		return String.format("[任务] - %s <耗时:%sms> (错误:%s)", new Object[]{
				DateTool.getNormalDateTime(startDate), getExecuteTime(),
				getErrorCount()});
	}

}
